package com.yg.controller;

import com.yg.pojo.TBook;
import com.yg.pojo.vo.PageVo;
import com.yg.service.TBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;


@Controller
public class IndexController {
    @Autowired
    private TBookService bookService;

    @GetMapping("/")
    public ModelAndView index(ModelAndView modelAndView) {
        PageVo<TBook> page = bookService.allBookPage(1L, 4L, "/index");
        System.out.println(page.toString());
        modelAndView.setViewName("index");
        modelAndView.addObject("page", page);
        return modelAndView;
    }

    @GetMapping("/index")
    public ModelAndView returnIndex(ModelAndView modelAndView) {
        return index(modelAndView);
    }

    @GetMapping("/index/page/{pageNo}/{pageSize}")
    public ModelAndView indexPage(@PathVariable("pageNo") Long pageNo, @PathVariable("pageSize") Long pageSize, ModelAndView modelAndView) {
        PageVo<TBook> page = bookService.allBookPage(pageNo, pageSize, "/index");
        modelAndView.setViewName("index");
        modelAndView.addObject("page", page);
        return modelAndView;
    }

    @PostMapping("/index/priceLimit")
    public ModelAndView priceLimit(@RequestParam("max") Integer max, @RequestParam("min") Integer min, ModelAndView modelAndView) {
        modelAndView.addObject("max", max);
        modelAndView.addObject("min", min);
        PageVo<TBook> page = bookService.bookByPricePage(max, min, 1L, 4L, "/index/priceLimit/" + min + "/" + max);
        modelAndView.addObject("page", page);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @GetMapping("/index/priceLimit/{min}/{max}/page/{pageNo}/{pageSize}")
    public ModelAndView priceLimitPage(@PathVariable("max") Integer max, @PathVariable("min") Integer min, @PathVariable("pageNo") Long pageNo, @PathVariable("pageSize") Long pageSize, ModelAndView modelAndView) {
        modelAndView.addObject("max", max);
        modelAndView.addObject("min", min);
        PageVo<TBook> page = bookService.bookByPricePage(max, min, pageNo, pageSize, "/index/priceLimit/" + min + "/" + max);
        modelAndView.addObject("page", page);
        modelAndView.setViewName("index");
        return modelAndView;
    }

    @GetMapping("/login")
    public ModelAndView login(ModelAndView modelAndView) {
        modelAndView.setViewName("user/login");
        return modelAndView;
    }

    @GetMapping("/regist")
    public ModelAndView regist(ModelAndView modelAndView) {
        modelAndView.setViewName("user/regist");
        return modelAndView;
    }
}
